package com.shenhaofeng.app.loader.content;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;

import com.shenhaofeng.app.loader.content.ContentSource;
import com.shenhaofeng.app.loader.content.OnBindContentListener;

import java.util.concurrent.Executor;

import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2016/9/7.
 */
public abstract class Component<R> {


    private Executor mExecutor;
    protected Looper mLooper;

    public Component() {
    }

    public Component(Executor mExecutor) {
        this.mExecutor = mExecutor;
    }

    public Component(Looper mLooper) {
        this.mLooper = mLooper;
    }

    protected Scheduler getWorkScheduler() {
        if (mExecutor != null) {
            return Schedulers.from(mExecutor);
        } else if (mLooper != null) {
            return AndroidSchedulers.from(mLooper);
        } else {
            return Schedulers.newThread();
        }
    }


    /**
     * 在首次加载数居前，对UI进行预处理
     *
     * @param statusView
     * @param contentView
     */
    protected abstract void onPreLoad(View statusView, View contentView);


    /**
     * 通过该方法触发数据加载事件，并处理相应结果
     *
     * @param statusView
     * @param contentView
     */
    protected abstract void onLoad(View statusView, View contentView, Bundle args, OnBindContentListener<R> listener, ContentSource<R> contentSource);


    /**
     * 在调用时重复调用加载，会导致加载中断,重新开始加载
     */
    protected abstract void onInterruptLoad(View statusView, View contentView);

    /**
     * 判断statusView和contentView是否能够切同合作
     *
     * @param statusView
     * @param contentView
     * @return
     */
    protected abstract boolean hasContentView(View statusView, View contentView);


}
